/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 **/


package com.cred.industries.platform.business.facade {
	
	import com.cred.industries.platform.business.TaskEvent;
	import com.cred.industries.platform.business.object.CustomerBO;
	import com.cred.industries.platform.dao.CustomerDAO;
	import com.cred.industries.platform.dao.event.CustomerDAOEvent;
	import com.cred.industries.platform.model.LocalUserData;
	import com.cred.industries.platform.transferobj.CustomerListTrans;
	
	import flash.events.EventDispatcher;

	[Event(name="TASK_COMPLETE", type="com.cred.industries.business.TaskEvent")]
	[Event(name="TASK_FAILED", type="com.cred.industries.business.TaskEvent")]

	/**
	 * @author Colin Redmond
	 * Copyright 2011
	 **/
	public final class CustomerFacade extends EventDispatcher {
		
		private var mCustomerDAO:CustomerDAO = new CustomerDAO();
		private var mCustlistTrans:CustomerListTrans = null;
		public function CustomerFacade() {	}
		
		public function get custFound():Array {
			
			return mCustlistTrans.customers;
		}
		
		public function hasNext():Boolean {
			
			return (mCustlistTrans.next.hRef != null && mCustlistTrans.next.hRef.length > 0);
		}
		
		public function getNext():void {
			
			if(!hasNext()) {
				dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILED, "we have no next set"));
				return;
			}
			var sessionKey:String = LocalUserData.get().sessionKey;
			mCustomerDAO.FindCustNextOrPrev(sessionKey, mCustlistTrans.next.hRef);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_GET_GROUP, customerFound, false, 0, true);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_FAILED, DAOFailed, false, 0, true);
		}
		
		public function hasPrevious():Boolean {
			
			return (mCustlistTrans.previous.hRef != null && mCustlistTrans.previous.hRef.length > 0);
		}
		
		public function getPrevious():void {
			
			if(!hasPrevious()) {
				
				dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILED, "we have no previous set"));
				return;
			}
			var sessionKey:String = LocalUserData.get().sessionKey;
			mCustomerDAO.FindCustNextOrPrev(sessionKey, mCustlistTrans.previous.hRef);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_GET_GROUP, customerFound, false, 0, true);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_FAILED, DAOFailed, false, 0, true);
		}

		public function createCustomer(cust:CustomerBO):void {
			
			//todo some validation first
			mCustomerDAO.createCustomer(cust);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_CREATED, customerCreated, false, 0, true);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_FAILED, DAOFailed, false, 0, true);
		}
		
		protected function customerCreated(event:CustomerDAOEvent):void {
			
			dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE));
		}
		
		protected function DAOFailed(event:CustomerDAOEvent):void {
			
			dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILED, event.message));
		}
		
		public function findCustomerByPersonaName(personaName:String):void {
			
			if(personaName.length < 3) {
				
				dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILED, "persona name must be at least three characters to search"));
				return;
			}

			var sessionKey:String = LocalUserData.get().sessionKey;
			mCustomerDAO.findCustomerByPersonaName(sessionKey, personaName);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_GET_GROUP, customerFound, false, 0, true);
			mCustomerDAO.addEventListener(CustomerDAOEvent.CUSTOMER_FAILED, DAOFailed, false, 0, true);
		}
		
		protected function customerFound(event:CustomerDAOEvent):void {
			
			mCustlistTrans = event.customersTrans;
			dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE));
		}
	}
}